package com.yj.springbootstudy;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

public class cashErrorOrder{


    /**
     * 只能用户yoopay渠道异常订单处理，其他渠道可自行扩展 vnp 5 ，cash 6
     * 数据格式 cash@@@YOOPAY2021101314272600533211013148192@@@5,000,000@@@CV09292946349370@@@2021-10-13 14:27:28
     * 时间支持两种格式，时间戳和时间格式
     * 金额支持两种格式，带逗号的和不带逗号的
     * @param args
     */
    public static void main(String[] args) {
        String payChannel = "yoopay";
        String sqlTemp = "INSERT INTO `sagittarius`.`pay_repayment_info`( `gid`, `order_id`, `loan_gid`, `user_gid`, `link`, `status`, `request_amount`, `trans_time`, `request_info`, `response_code`, `response_body`, `expire_time`, `pay_channel`, `acc_no`, `enable_flag`, `channel`, `sub_channel`, `create_time`, `update_time`, `ext1`, `ext2`, `ext3`, `ext4`, `trans_id`) \n" +
                "select replace(uuid(),\"-\",\"\"),'${transId}',t.gid,t.user_gid,null,1,${tansAmount},${transTime},null,1,'shou dong',null,${rapayChannel},t2.acc_no,'Y',t.channel, t.sub_channel, ${transTime}, ${transTime}, NULL, NULL, NULL, NULL,'${transId}' from gemini.record_loan t  inner join gemini.virtual_account t2 on t.order_id = t2.order_id \n" +
                " where t.order_id = '${orderId}' and t2.repay_channel = '"+payChannel+"' limit 1;";

        try {
            String encoding = "GBK";
            String filePath = "C:\\Users\\yj\\Desktop\\工作\\异常订单生成sql.txt";
            File file = new File(filePath);

            if (file.isFile() && file.exists()) { //判断文件是否存在
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(file), encoding);//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                while ((lineTxt = bufferedReader.readLine()) != null) {
                    //解析凭借sql
                    String[] s = lineTxt.split("@@@");
                    String pro = s[0].trim();
                    String repayChannel = "6";
                    if ("vnp".equalsIgnoreCase(pro)){
                        repayChannel = "5";
                    }
                    String transId = s[1].trim();
                    String transAmount = s[2].trim().replace(",","");
                    String orderId = s[3].trim();
                    String transData = s[4].trim();
                    Integer transTimestamp = 0;
                    //如果不是时间戳，再用时间格式去转换
                    try {
                        transTimestamp = Integer.valueOf(transData);
                    } catch (NumberFormatException e) {
                        LocalDateTime localDateTime = ofPattern_YYYY_MM_DD_HH_MM_SS(transData);
                        transTimestamp = getTimeStampFromDate(localDateTime, "+7");
                    }
                    String replace = sqlTemp.replace("${rapayChannel}",repayChannel)
                            .replace("${transId}", transId)
                            .replace("${tansAmount}",transAmount)
                            .replace("${transTime}",transTimestamp.toString()).replace("${orderId}",orderId);
                    System.out.println(replace);
//                    System.out.println(transId + transAmount+orderId+"|"+timeStampFromDate);
                }
                read.close();
            } else {
                System.out.println("找不到指定的文件");
            }
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }
    }

    /**
     * localDateTime -> timestamp (s)
     * @param dateTime
     * @param zoneOffset  eg: "+8"/"+7"
     * @return
     */
    public static Integer getTimeStampFromDate(LocalDateTime dateTime, String zoneOffset) {
        if (dateTime == null) {
            return null;
        }
        return (int) (dateTime.toInstant(ZoneOffset.of(zoneOffset)).toEpochMilli() / 1000);
    }

    public static LocalDateTime ofPattern_YYYY_MM_DD_HH_MM_SS(String dateStr) {
        return LocalDateTime.parse(dateStr, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }
}